package com.zou.lotterylucky.activity_new

import android.app.Activity
import android.view.MotionEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity

/**
 * 描述：activity基类
 * <p>
 * 作者：王俊
 * 日期：2021/5/30
 */
open class BaseActivity : AppCompatActivity() {

//    /**
//     * 触摸监听
//     */
//    override fun dispatchTouchEvent(event: MotionEvent): Boolean {
//        if (MotionEvent.ACTION_DOWN == event.action) {
//            val view = currentFocus
//            if (view != null) {
//                hideKeyboard(event, view, this)
//            }
//        }
//        return super.dispatchTouchEvent(event)
//    }

    /**
     * 隐藏键盘
     */
    open fun hideKeyboard(event: MotionEvent, view: View, activity: Activity) {
        try {
            if (view is EditText) {
                val location = intArrayOf(0, 0)
                view.getLocationInWindow(location)
                val left = location[0]
                val top = location[1]
                val right = left + view.getWidth()
                val bottom = top + view.getHeight()
                // 判断焦点位置坐标是否在空间内，如果位置在控件外，则隐藏键盘
                if (event.rawX < left || event.rawX > right || event.y < top || event.rawY > bottom) {
                    // 隐藏键盘
                    val token = view.getWindowToken()
                    val inputMethodManager =
                        activity.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
                    inputMethodManager.hideSoftInputFromWindow(
                        token,
                        InputMethodManager.HIDE_NOT_ALWAYS
                    )
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}